import gtk

from kiwi.ui.delegates import GladeSlaveDelegate, GladeDelegate
from kiwi.ui.dialogs import warning

from otica.views.baseeditor import BaseEditor
from otica.models.clientes import Cliente


cliente_widgets = ['codigo','nome','data','ultimacompra','contato','datanasc','estadocivil', 
           'esposa','rg','cpf','spc','endereco','bairro','cidade','cep',
           'estado','telefone1','telefone2','fax','email','pai','mae']


class   ClienteEditor(BaseEditor):
    
    
    def __init__(self, excluir):
        
        
        BaseEditor.__init__(self,excluir,Cliente,'codigo', ClienteSlave, cliente_widgets)
        self.localizar = None

        
    def configura_editor(self):
        items = [('Casado','Casado'), ('Solteiro','Solteiro')]
        self.editor.estadocivil.prefill(items)        
        
    def set_editable_widgets(self,editable):
        self.editor.codigo.set_editable(editable)
        self.editor.nome.set_editable(editable)
        self.editor.data.set_sensitive(editable)
        self.editor.ultimacompra.set_sensitive(editable)
        self.editor.contato.set_sensitive(editable)
        self.editor.datanasc.set_sensitive(editable)
        self.editor.estadocivil.set_sensitive(editable)
        self.editor.esposa.set_editable(editable)
        self.editor.rg.set_editable(editable)
        self.editor.cpf.set_editable(editable)
        self.editor.spc.set_editable(editable)
        self.editor.endereco.set_editable(editable)
        self.editor.bairro.set_editable(editable)
        self.editor.cidade.set_editable(editable)
        self.editor.cep.set_editable(editable)
        self.editor.estado.set_editable(editable)
        self.editor.telefone1.set_editable(editable)
        self.editor.telefone2.set_editable(editable)
        self.editor.fax.set_editable(editable)
        self.editor.email.set_editable(editable)
        self.editor.pai.set_editable(editable)
        self.editor.mae.set_editable(editable)
        
    def on_novo_action__activate(self, action):
        
        BaseEditor.on_novo_action__activate(self, action)
        self.editor.codigo.grab_focus()

    def on_editar_action__activate(self, action):
        BaseEditor.on_editar_action__activate(self, action)
        self.editor.codigo.grab_focus()
        
    def on_localizar_action__activate(self, action):
        BaseEditor.on_localizar_action__activate(self, action)
        if not self.localizar:
           self.localizar = BuscaCliente(self)
        self.localizar.set_transient_for(self.baseeditor)
        self.localizar.buscacliente.move(380,610)
        self.localizar.texto.grab_focus()
        self.localizar.show_all()
        
                
    
    def search(self, texto,arg):
        self.set_new_transaction()
        result = self.classModel.searchOne(texto,arg, self.trans)
        if result:
            self.model = self.listModels.set_list_pos(result.id)
            self.proxy.set_model(self.model)
        else:
            warning('Nenhum registro encontrado!',parent=self.baseeditor)
           
            

class ClienteSlave(GladeSlaveDelegate):
    def __init__(self):
        GladeSlaveDelegate.__init__(self,gladefile="clienteeditor")



class BuscaCliente(GladeDelegate):
    def __init__(self, domain):
        
        keyactions = { gtk.keysyms.Return : self.enter,
                      gtk.keysyms.KP_Enter : self.enter }
        
        GladeDelegate.__init__(self,domain=domain,
                          gladefile="buscacliente",
                          keyactions=keyactions)
        self.buscacliente.set_deletable(False)
        self.buscacliente.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
        
            
    
    def enter(self):
        self.on_buscar__pressed(None)

    def on_codigo_check__content_changed(self,s):
    
        self.texto.grab_focus()

    def on_nome_check__content_changed(self,s):
    
        self.texto.grab_focus()

    def on_cpf_check__content_changed(self,s):
    
        self.texto.grab_focus()

    def on_rg_check__content_changed(self,s):
    
        self.texto.grab_focus()
    
    def on_buscar__pressed(self,action):
        texto = self.texto.get_text() 
        if texto == '':
            warning('Preencha o texto',parent=self.buscacliente)
        else:
            arg = None
            if self.codigo_check.get_active():
                arg = 'codigo'
            elif self.nome_check.get_active():
                arg = 'nome'
            elif self.rg_check.get_active():
                arg = 'rg'
            else:
                arg = 'cpf'
            self.domain.search(texto, arg)

    def on_fechar__pressed(self,action):
        
        self.buscacliente.hide()
        self.domain.localizarFunc(True)
    
    '''def on_buscacliente__focus_out_event(self,componente,event):
        
        if self.texto.get_text():
            self.domain.baseeditor.set_modal(True)
        else:
            self.domain.baseeditor.set_modal(False)
    
    def on_buscacliente__focus_in_event(self,componente,event):
        
        self.domain.baseeditor.set_modal(False)'''
        
        
                 
        

    